package com.wiseMen.wechat.user;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.sword.wechat4j.user.User;
import org.sword.wechat4j.util.WeChatUtil;

import com.wiseMen.core.dao.BaseEntity;

/**
 * 微信用户信息
 * @author 吴人龙
 */
@Table(name = "WC_USER")
public class WechatUserEntity implements BaseEntity{

	private static final long serialVersionUID = 5511678137001110767L;

	private String id;//用户的标识，对当前公众号唯一（OPENID）
	private Integer subscribe;//用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息。
	private String nickName;//用户的昵称
	private Integer sex;//用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
	private String city;//用户所在城市
	private String country;//用户所在国家
	private String province;//用户所在省份
	private String language;//用户的语言，简体中文为zh_CN
	private String headImgUrl;//用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空。若用户更换头像，原有头像URL将失效。
	@Column(name = "SUBSCRIBE_TIME")
	private Date subscribeTime;//用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间
	private String unionId;//只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。
	private String remark;//公众号运营者对粉丝的备注，公众号运营者可在微信公众平台用户管理界面对粉丝添加备注
	private Integer groupId;//用户所在的分组ID（兼容旧的用户分组接口）
	@Column(name = "TAGID_LIST")
	private String tagidList;//用户被打上的标签ID列表
	private String remarkName;//备注名
	private Date syncTime;//最后同步时间
	@Column(name = "NICKNAME_ENCODE")
	private String nicknameEncode;//存储ISO8859-1编码的昵称，主要解决emoji表情符乱码
	
	public WechatUserEntity(){
		super();
	}
	
	public WechatUserEntity(User user){
		if(user!=null){
			this.id = user.getOpenId();
			this.subscribe = user.getSubscribe();
			this.subscribeTime = WeChatUtil.timeStampToDate(user.getSubscribeTime());
			this.nicknameEncode = user.getNickName();
			this.nickName = WeChatUtil.toUtf8(user.getNickName());
			if(WeChatUtil.isEmoji(this.nickName)){
				this.nickName = "";
			}
			this.sex = user.getSex();
			this.city = WeChatUtil.toUtf8(user.getCity());
			this.country = WeChatUtil.toUtf8(user.getCountry());
			this.province = WeChatUtil.toUtf8(user.getProvince());
			this.language = user.getLanguage().name();
			this.headImgUrl = user.getHeadimgUrl();
			this.unionId = user.getUnionId();
			this.remark = WeChatUtil.toUtf8(user.getRemark());
			this.groupId = user.getGroupId();
			this.tagidList = WeChatUtil.toUtf8(user.getTagidList()).replace("[", "").replace("]", "");
		}
	}
	@Transient
	public String getNickNameDecode() {
		if(StringUtils.isNotBlank(this.nickName)){
			return this.nickName;
		}else{
			return WeChatUtil.toUtf8(this.nicknameEncode);
		}
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public Integer getSubscribe() {
		return subscribe;
	}
	public void setSubscribe(Integer subscribe) {
		this.subscribe = subscribe;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public Integer getSex() {
		return sex;
	}
	public void setSex(Integer sex) {
		this.sex = sex;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getLanguage() {
		return language;
	}
	public void setLanguage(String language) {
		this.language = language;
	}
	public String getHeadImgUrl() {
		return headImgUrl;
	}
	public void setHeadImgUrl(String headImgUrl) {
		this.headImgUrl = headImgUrl;
	}
	public Date getSubscribeTime() {
		return subscribeTime;
	}
	public void setSubscribeTime(Date subscribeTime) {
		this.subscribeTime = subscribeTime;
	}
	public String getUnionId() {
		return unionId;
	}
	public void setUnionId(String unionId) {
		this.unionId = unionId;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public Integer getGroupId() {
		return groupId;
	}
	public void setGroupId(Integer groupId) {
		this.groupId = groupId;
	}
	public String getTagidList() {
		return tagidList;
	}
	public void setTagidList(String tagidList) {
		this.tagidList = tagidList;
	}
	public Date getSyncTime() {
		return syncTime;
	}
	public void setSyncTime(Date syncTime) {
		this.syncTime = syncTime;
	}
	public String getRemarkName() {
		return remarkName;
	}
	public void setRemarkName(String remarkName) {
		this.remarkName = remarkName;
	}
	public String getNicknameEncode() {
		return nicknameEncode;
	}
	public void setNicknameEncode(String nicknameEncode) {
		this.nicknameEncode = nicknameEncode;
	}
}
